package com.qidian.mes.sys.controller;

import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;

/**
 * @Author 戴着假发的程序员
 * @Company 起点编程
 * @Time 2023/10/26 11:51
 * @Description
 */
@RestController
@CrossOrigin
@RequestMapping("/gen")
public class DemoController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping
    public MESResult gen(){
        String key = "XXXID";
        // 生产编号的业务应该写在service中，我们这里因为时间关系，就直接写这里
        StringBuilder id = new StringBuilder();
        // 固定部分-可能需要从数据库查询
        id.append("641223");
        // 时间
        Calendar calendar = Calendar.getInstance();
        id.append(calendar.get(Calendar.YEAR) + "" + (calendar.get(Calendar.MONTH) + 1) + calendar.get(Calendar.DATE));
        //  从redis获取序列
        Long idTemp = stringRedisTemplate.opsForValue().increment(key);
        String idStr = idTemp.toString();
        if(idStr.length() < 5){
            // 前面加0
            for (int i = 0;i < 5 - idStr.length();i++){
                idStr = "0" + idStr;
            }
        }
        id.append(idStr);
        return MESResult.ok(id);
    }
}
